home *** CD-ROM | disk | FTP | other *** search
/ Amiga Game-Power / Amiga Game-Power.iso / anwendungen / mackie / nethack / termcap.zoo / tgetstr.c < prev    next >
C/C++ Source or Header  |  1988-07-30  |  6KB  |  284 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1982, Fred Fish                   *
  4.  *                All Rights Reserved             *
  5.  *                                    *
  6.  *    This software and/or documentation is released for public    *
  7.  *    distribution for personal, non-commercial use only.        *
  8.  *    Limited rights to use, modify, and redistribute are hereby    *
  9.  *    granted for non-commercial purposes, provided that all        *
  10.  *    copyright notices remain intact and all changes are clearly    *
  11.  *    documented.  The author makes no warranty of any kind with    *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular    *
  14.  *    purpose.                            *
  15.  *                                    *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  LIBRARY FUNCTION
  22.  *
  23.  *    tgetstr   extract string capability from termcap entry
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    termcap
  28.  *
  29.  *  SYNOPSIS
  30.  *
  31.  *    char *tgetstr(id,area)
  32.  *    char *id;
  33.  *    char **area;
  34.  *
  35.  *  DESCRIPTION
  36.  *
  37.  *    Gets the string capability for <id>, placing it in
  38.  *    the buffer at *area, and advancing *area to point
  39.  *    to next available storage.
  40.  *
  41.  *    For example, if the following capabilities are
  42.  *    in the termcap file:
  43.  *
  44.  *        ZZ=zzzz
  45.  *        YY=yyyyyy
  46.  *        WW=www
  47.  *
  48.  *    then successive calls using YY, ZZ, and WW will
  49.  *    build the following buffer:
  50.  *
  51.  *        yyyyyy0zzzz0www0
  52.  *
  53.  *    The first call will return a pointer to yyyyyy, the
  54.  *    second will return a pointer to zzzz and the third
  55.  *    will return a pointer to www.  Note that each
  56.  *    string is null terminated, as are all C strings.
  57.  *
  58.  *    Characters preceded by the carot character (\136)
  59.  *    are mapped into the corresponding control character.
  60.  *    For example, the two character sequence ^A becomes
  61.  *    a single control-A (\001) character.
  62.  *
  63.  *    The escape character is the normal C backslash and
  64.  *    the normal C escape sequences are recognized, along
  65.  *    with a special sequence for the ASCII escape character
  66.  *    (\033).  The recognized sequences are:
  67.  *
  68.  *        \E   =>  '\033'  (ASCII escape character)
  69.  *        \b   =>  '\010'  (ASCII backspace character)
  70.  *        \f   =>  '\014'  (ASCII form feed character)
  71.  *        \n   =>  '\012'  (ASCII newline/linefeed char)
  72.  *        \r   =>  '\015'  (ASCII carriage return char)
  73.  *        \t   =>  '\011'  (ASCII tab character)
  74.  *        \ddd =>  '\ddd'  (arbitrary ASCII digit)
  75.  *        \x   =>  'x'     (ordinary ASCII character)
  76.  *
  77.  */
  78.  
  79. #include <stdio.h>
  80. #include <string.h>
  81. # ifdef MSDOS
  82. # define index strchr
  83. # endif
  84.  
  85. extern char *_tcpbuf;        /* Termcap entry buffer pointer */
  86.  
  87. /*
  88.  *  PSEUDO CODE
  89.  *
  90.  *    Begin tgetstr
  91.  *        Initialize pointer to the termcap entry buffer.
  92.  *        While there is a field to process
  93.  *        Skip over the field separator character.
  94.  *        If this is the entry we want then
  95.  *            If the entry is not a string then
  96.  *            Return NULL.
  97.  *            Else
  98.  *            Transfer string and rtn pointer.
  99.  *            End if
  100.  *        End if
  101.  *        End while
  102.  *        Return NULL
  103.  *    End tgetstr
  104.  *
  105.  */
  106.  
  107. char *tgetstr(id,area)
  108. char *id;
  109. char **area;
  110. {
  111.     char *bp;
  112.     extern char *index();
  113.     char *decode();
  114.  
  115.     bp = _tcpbuf;
  116.     while ((bp = index(bp,':')) != NULL) {
  117.     bp++;
  118.     if (*bp++ == id[0] && *bp != NULL && *bp++ == id[1]) {
  119.         if (*bp != NULL && *bp++ != '=') {
  120.         return(NULL);
  121.         } else {
  122.         return(decode(bp,area));
  123.         }
  124.     }
  125.     }
  126.     return(NULL);
  127. }
  128.  
  129. /*
  130.  *  INTERNAL FUNCTION
  131.  *
  132.  *    decode     transfer string capability, decoding escapes
  133.  *
  134.  *  SYNOPSIS
  135.  *
  136.  *    static char *decode(bp,area)
  137.  *    char *bp;
  138.  *    char **area;
  139.  *
  140.  *  DESCRIPTION
  141.  *
  142.  *    Transfers the string capability, up to the next ':'
  143.  *    character, or null, to the buffer pointed to by
  144.  *    the pointer in *area.  Note that the initial
  145.  *    value of *area and *area is updated to point
  146.  *    to the next available location after the null
  147.  *    terminating the transfered string.
  148.  *
  149.  *  BUGS
  150.  *
  151.  *    There is no overflow checking done on the destination
  152.  *    buffer, so it better be large enough to hold
  153.  *    all expected strings.
  154.  *
  155.  */
  156.  
  157. /*
  158.  *  PSEUDO CODE
  159.  *
  160.  *    Begin decode
  161.  *        Initialize the transfer pointer.
  162.  *        While there is an input character left to process
  163.  *        Switch on input character
  164.  *        Case ESCAPE:
  165.  *            Decode and xfer the escaped sequence.
  166.  *            Break
  167.  *        Case CONTROLIFY:
  168.  *            Controlify and xfer the next character.
  169.  *            Advance the buffer pointer.
  170.  *            Break
  171.  *        Default:
  172.  *            Xfer a normal character.
  173.  *        End switch
  174.  *        End while
  175.  *        Null terminate the output string.
  176.  *        Remember where the output string starts.
  177.  *        Update the output buffer pointer.
  178.  *        Return pointer to the output string.
  179.  *    End decode
  180.  *
  181.  */
  182.  
  183. static char *decode(bp,area)
  184. char *bp;
  185. char **area;
  186. {
  187.     char *cp, *bgn;
  188.     char *do_esc();
  189.  
  190.     cp = *area;
  191.     while (*bp != NULL && *bp != ':') {
  192.     switch(*bp) {
  193.     case '\\':
  194.         bp = do_esc(cp++,++bp);
  195.         break;
  196.     case '^':
  197.         *cp++ = *++bp & 037;
  198.         bp++;
  199.         break;
  200.     default:
  201.         *cp++ = *bp++;
  202.         break;
  203.     }
  204.     }
  205.     *cp++ = NULL;
  206.     bgn = *area;
  207.     *area = cp;
  208.     return(bgn);
  209. }
  210.  
  211. /*
  212.  *  INTERNAL FUNCTION
  213.  *
  214.  *    do_esc      process an escaped sequence
  215.  *
  216.  *  SYNOPSIS
  217.  *
  218.  *    char *do_esc(out,in);
  219.  *    char *out;
  220.  *    char *in;
  221.  *
  222.  *  DESCRIPTION
  223.  *
  224.  *    Processes an escape sequence pointed to by
  225.  *    in, transfering it to location pointed to
  226.  *    by out, and updating the pointer to in.
  227.  *
  228.  */
  229.  
  230. /*
  231.  *  PSEUDO CODE
  232.  *
  233.  *    Begin do_esc
  234.  *        If the first character is not a NULL then
  235.  *        If is a digit then
  236.  *            Set value to zero.
  237.  *            For up to 3 digits
  238.  *            Accumulate the sum.
  239.  *            End for
  240.  *            Transfer the sum.
  241.  *        Else if character is in remap list then
  242.  *            Transfer the remapped character.
  243.  *            Advance the input pointer once.
  244.  *        Else
  245.  *            Simply transfer the character.
  246.  *        End if
  247.  *        End if
  248.  *        Return updated input pointer.
  249.  *    End do_esc
  250.  *
  251.  */
  252.  
  253. static char *maplist = {
  254.     "E\033b\bf\fn\nr\rt\t"
  255. };
  256.  
  257. char *do_esc(out,in)
  258. char *out;
  259. char *in;
  260. {
  261.     int count;
  262.     char ch;
  263.     char *cp;
  264.     extern char *index();
  265.     extern int isdigit();
  266.  
  267.     if (*in != NULL) {
  268.     if (isdigit(*in)) {
  269.         ch = 0;
  270.         for (count = 0; count < 3 && isdigit(*in); in++) {
  271.          ch <<= 3;
  272.          ch |= (*in - '0');
  273.         }
  274.         *out++ = ch;
  275.     } else if ((cp = index(maplist,*in)) != NULL) {
  276.         *out++ = *++cp;
  277.         in++;
  278.     } else {
  279.         *out++ = *in++;
  280.     }
  281.     }
  282.     return(in);
  283. }
  284.